<template>
  <div id='electronic' class="set">
    <Row type="flex" align="middle" justify="center" class="set_ct">
      <Col>
        <Form ref="formDynamic" :model="formDynamic" :rules="ruleValidate" :label-width="180">
          <FormItem label="是否采用全电子币经营模式：" prop='radio'>
            <RadioGroup v-model="formDynamic.radio">
              <Radio label="1" style="margin-left:35px">是</Radio>
              <Radio label="0" style="margin-left:35px">否</Radio>
            </RadioGroup>
          </FormItem>
          <FormItem>
            <Button type="primary"  @click="handleSubmit" class="button">确定</Button>
            <Button @click="cancel" class="button btn1">取消</Button>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </div>
</template>

<script>
import './index.less'
import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  data () {
    return {
      formDynamic: {
        radio: ''
      },
      ruleValidate: {
        radio: { required: true, message: '请选择是否采用全电子币经营模式', trigger: 'blur' }
      },
      paramter: {
        'merchantId': this.$store.state.user.merchantId,
        'msType': '0'
      }
    }
  },
  mounted () {
    let obj = Object.assign({}, this.paramter, { 'dictKeys': 'electronicCoin' })
    getDicQuery(obj).then((res) => {
      if (res.data.status === 200) {
        this.formDynamic.radio = res.data.data[0].electronicCoin + ''
      } else {
        this.$Message.error(`${res.data.error}`)
      }
    })
    if (!this.hasPermission('moms:globalsetting:virtualcoin')) {
      this.$router.replace({ name: 'error_401' })
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.formDynamic.validate((valid) => {
        if (valid) {
          let data = Object.assign({}, this.paramter, { 'electronicCoin': this.formDynamic.radio })
          setDicSave(data).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success('设置成功!')
              this.closeModal()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          })
        }
      })
    },
    cancel () {
      this.closeModal()
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'global_setting'
        })
      })
    }
  }
}
</script>
